package com.web;

import java.io.*;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * User: wangchen
 * Date: 16-4-24
 * Time: 下午5:49
 * To change this template use File | Settings | File Templates.
 */
public class Processor extends  Thread{
    private  Socket socket;
    private InputStream in;
    private PrintStream out;
    public final static String WEB_ROOT="d:\\web";
    public Processor(Socket socket){
           this.socket = socket;
        try{
            in = socket.getInputStream();
            out =new PrintStream( socket.getOutputStream());
        }catch (IOException e){
            e.printStackTrace();
        }

    }
    public void run(){
         String filename = parse(in);
        sendFile(filename);
    }

    public String parse(InputStream in){
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        try {
           String httpMessage = br.readLine();
           String[] content = httpMessage.split(" ");//0 状态号 1请求名称 2版本号
            if(content==null || content.length!=3){
                sendErrorMessage(400,"客户端请求错误");
                return null;
            }
            System.out.println("code:"+content[0]+",filename:"+content[1]+", version:"+content[2]);
           return content[1];
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }

    public void  sendErrorMessage(int code,String msg){
        out.println("HTTP/1.0 "+code+" "+msg);
        out.println("content-type:text/html");
        out.println();
        out.println("<html>");
        out.println("<head>");
        out.println("<meta charset=\"UTF-8\">");
        out.println("</head>");
        out.println("<body>");
        out.println("errorCode:"+code);
        out.println("<br/>");
        out.println("错误信息："+msg);

        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    public void sendFile(String filename){
                System.out.println("WEB_ROOT+filename::"+WEB_ROOT+filename);
        File file = new File(WEB_ROOT+filename);
        if(!file.exists()){
            sendErrorMessage(404,"访问的文件不存在");
            return;
        }
        try {
            InputStream in = new FileInputStream(file);
            byte content[] = new byte[(int) file.length()];
            try {
                in.read(content);
                out.println("HTTP/1.0 200 query");
                out.println("content-length:" + content.length);
                out.println();
                out.write(content);
                out.flush();
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }
}
